package com.banshuibao.bsb_android.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.banshuibao.bsb_android.R;
import com.banshuibao.bsb_android.model.O2oModel;
import com.banshuibao.bsb_android.util.ImageUtil;
import com.banshuibao.bsb_android.util.IntentManager;
import com.banshuibao.bsb_android.util.PriceUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangpan on 2017/12/23.
 */

public class O2oAdapter extends BaseAdapter {
    Context context;

    List<O2oModel.DataBean> list ;

    public O2oAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setUpData(List<O2oModel.DataBean> data){
        if(this.list!=null){
            this.list.addAll(data);
            notifyDataSetChanged();
        }
    }

    public void setDownData(List<O2oModel.DataBean> data){
        if(this.list!=null){
            this.list.clear();
            this.list.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getCount() {
        return list!=null?list.size():0;
    }

    @Override
    public O2oModel.DataBean getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null){
            view = View.inflate(context, R.layout.counsoler_list_item_layout,null);
            viewHolder = new ViewHolder();
            viewHolder.head_iv = view.findViewById(R.id.head_iv);
            viewHolder.name = view.findViewById(R.id.name);
            viewHolder.post = view.findViewById(R.id.post);
            viewHolder.price = view.findViewById(R.id.price);
            view.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) view.getTag();
        }
        ImageUtil.loadImageHead(context, list.get(i).getHeadImg(), viewHolder.head_iv);
        viewHolder.name.setText(list.get(i).getName());

        if(list.get(i).getRole().equals("1")){
            viewHolder.post.setText(list.get(i).getPositiotn());
        }else {
            if(!TextUtils.isEmpty(list.get(i).getDuty())){
                viewHolder.post.setText(list.get(i).getDuty());
            }else{
                viewHolder.post.setText(list.get(i).getPositiotn());
            }
        }
        String price = PriceUtil.FormatDecimal(list.get(i).getAdvisoryPrice());
        viewHolder.price.setText("￥ "+price+"/次 咨询");
        viewHolder.price.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                IntentManager.goQuestionCloselyActivity(context,list.get(i).getUuid(),list.get(i).getAdvisoryPrice(),"o2o");
            }
        });
        return view;
    }

    class ViewHolder{
        ImageView head_iv;
        TextView name,post,price;
    }

    public void clear(){
        if(list!=null){
            list.clear();
        }
    }
}
